package DAO;

import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Model.EvolucaoClinica;
import Utils.Conexao;

public class EvolucaoClinicaDAO {
	public static void Insert(EvolucaoClinica objEvClinica) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_insert_evolucao_clinica(?, ?, ?, ?, ?)";			
		CallableStatement call = EvolucaoClinicaDAO.setObjectCallableStatement(objEvClinica, mysqlCall, con);
		call.execute();
		call.close();
		con.close();		
	}
	
	public static void Update(EvolucaoClinica objEvClinica) throws SQLException
	{		
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_update_evolucao_clinica (?, ?, ?, ?, ?, ?)";		
		CallableStatement call = EvolucaoClinicaDAO.setObjectCallableStatement(objEvClinica, mysqlCall, con) ;
		call.execute();
		call.close();
		con.close();
	}
	
	public static void InativarEvolucaoClinica(int idEvolucao) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_inativar_evolucao_clinica(?)";
		CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, idEvolucao);
		call.execute();
		
		call.close();		
		con.close();
	}
	
	
	public static ResultSet relatorioEvolucaoClinica(java.sql.Date data_inicio, java.sql.Date data_fim) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_relatorio_evolucao(?, ?)";
		CallableStatement call = con.prepareCall(mysqlCall);
		call.setDate(1, data_inicio);
		call.setDate(2, data_fim);		

		return call.executeQuery();
	}
	
	public static ArrayList<EvolucaoClinica> GetArrayListEvolucaoClinica()
	{
		Connection con = Conexao.getConection();
		ArrayList<EvolucaoClinica> arrEvolucao = new ArrayList<EvolucaoClinica>();
		String mysqlCall = "call spc_select_evolucoes_clinicas()";
		try
		{			
			CallableStatement call =(CallableStatement) con.prepareCall(mysqlCall);
			ResultSet res = call.executeQuery();
			while (res.next()) {		
				EvolucaoClinica evolucao = EvolucaoClinicaDAO.setInstance(res);
				arrEvolucao.add(evolucao);
			}
				
			con.close();
			call.close();
		}
		catch(SQLException ex)
		{
			ex.printStackTrace();
		}
		
		return arrEvolucao;		
	}
	
	public static EvolucaoClinica LoadObject(int idEvolucao) throws SQLException
	{
		Connection con = Conexao.getConection();
		EvolucaoClinica evolucao = new EvolucaoClinica();
		String mysqlcall = "call spc_select_evolucao_clinica_por_id(?);";
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlcall);
		call.setInt(1, idEvolucao);
		ResultSet res = call.executeQuery();
		if(res.next())
			evolucao = EvolucaoClinicaDAO.setInstance(res);
		
		call.close();
		con.close();
		return evolucao; 
	}
	
	private static CallableStatement setObjectCallableStatement(EvolucaoClinica objEvClinica, String mysqlCall, Connection con ) throws SQLException
	{		
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
		call.setDate(1, objEvClinica.getDataEvolucao());
		call.setString(2, objEvClinica.getAssunto());
		call.setString(3, objEvClinica.getObservacao());
		call.setString(4, objEvClinica.getArea());
		call.setInt(5, objEvClinica.getIdPaciente());
		
		if(objEvClinica.getIdEvolucaoClinica() > 0)
			call.setInt(6, objEvClinica.getIdEvolucaoClinica());
	
		return call; 		
	}
	
	private static EvolucaoClinica setInstance(ResultSet res) throws SQLException
	{
		EvolucaoClinica evolucao = new EvolucaoClinica();
		evolucao.setIdEvolucaoClinica(res.getInt("id_evolucao_clinica"));
		evolucao.setDataEvolucao(res.getDate("data_evolucao"));
		evolucao.setAssunto(res.getString("assunto"));
		evolucao.setObservacao(res.getString("observacao"));
		evolucao.setArea(res.getString("area"));
		evolucao.setIdPaciente(res.getInt("id_paciente"));
		evolucao.setPaciente(PacienteDAO.LoadObject(res.getInt("id_paciente")));
		return evolucao;
	}	
}
